/**
  ******************************************************************************
  * @file    xxx.c 
  * @author  SD Electronics xxx
  * @version V1.0
  * @date    xx-xx-xx
  * @brief   xxxxxxxxxxxxxxxxxx.
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "sd_common.h"

/** 
  * @brief  LED 硬件配置相关结构体 
  */ 
ST_LED_PARAM stLEDParam[EN_LED_MAX] = {
    {GPIO_PORT_D, GPIO_BIT_5},
    {GPIO_PORT_D, GPIO_BIT_6},
};

/** 
  * @brief  LED 硬件配置相关结构体 
  */ 
ST_LED_PHY g_stLEDPhyParam = 
{
    stLEDParam
};


/** 
  * @brief LED 控制结构体 
  */ 
ST_LED_CTRL g_stLEDCtrl = 
{                       
    false,
    &g_stLEDPhyParam,
};  

/**
  * @}
  */


/**
  * @brief  SDInitLED
  * @note   xxx
  * @param  None
  * @retval None
  */
void SDInitLED(PST_LED_PHY pstLEDCntPhy)
{
    pmu_set_port_mux(GPIO_PORT_D, GPIO_BIT_6, PMU_PORT_MUX_GPIO);
	pmu_set_port_mux(GPIO_PORT_D, GPIO_BIT_5, PMU_PORT_MUX_GPIO);
    pmu_set_pin_to_PMU(GPIO_PORT_D, BIT(5) | BIT(6));
    pmu_set_pin_dir(GPIO_PORT_D, BIT(5) | BIT(6), GPIO_DIR_OUT);
    pmu_set_pin_pull(GPIO_PORT_D, BIT(5) | BIT(6), true);
    //set H level
    pmu_set_gpio_value(GPIO_PORT_D, BIT(5), 1);
    //set H level
    pmu_set_gpio_value(GPIO_PORT_D, BIT(6), 1);

    //背光灯
    pmu_set_led2_value(0);
    printf("usr: sd LED init.\r\n");
}

/**
  * @brief  LEDOpen
  * @note   xxx
  * @param  None
  * @retval None
  */
static long LEDOpen(void *pvHandle, unsigned long ulOption)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;

    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        PST_LED_CTRL pstLEDCtrl = (PST_LED_CTRL)pstDevManage->pvData;
        if(false == pstLEDCtrl->bInitFlag)
        {
			//初始化
            SDInitLED(pstLEDCtrl->pstLEDInfo);
            pstLEDCtrl->bInitFlag = true;
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;
}  

/**
  * @brief  LEDRead
  * @note   xxx
  * @param  None
  * @retval None
  */
static long LEDRead(void *pvHandle, unsigned long ulOpertion, unsigned char *pucDataBuf, int* ulDataSize)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        PST_LED_CTRL pstLEDCtrl = (PST_LED_CTRL)pstDevManage->pvData;
        //
        if(true == pstLEDCtrl->bInitFlag)
        {
            ;
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;    
}

/**
  * @brief  LEDWrite
  * @note   xxx
  * @param  None
  * @retval None
  */
static long LEDWrite(void *pvHandle, unsigned long ulOpertion, unsigned char *pucDataBuf, unsigned long ulDataLen)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        PST_LED_CTRL pstLEDCtrl = (PST_LED_CTRL)pstDevManage->pvData;
        //
        if(true == pstLEDCtrl->bInitFlag)
        {
           ;
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;
}

/**
  * @brief  LEDControl
  * @note   xxx
  * @param  None
  * @retval None
  */
static long LEDControl (void *pvHandle, unsigned long ulOpertion, unsigned long ulControlCmd, void *pvd)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    { 
        PST_LED_CTRL pstLEDCtrl = (PST_LED_CTRL)pstDevManage->pvData;
        //
        if(true == pstLEDCtrl->bInitFlag)
        {
            switch(ulOpertion)
            {
                case EN_LED_ON:
                //printf("[EN_LED_ON]\r\n");
                //set H level
	            pmu_set_gpio_value(GPIO_PORT_D, BIT(5)|BIT(6), 0);
                //set H level
	            //pmu_set_gpio_value(GPIO_PORT_D, BIT(6), 0);
                break;

                case EN_LED_OFF:
                //printf("[EN_LED_OFF]\r\n");
                //set H level
	            pmu_set_gpio_value(GPIO_PORT_D, BIT(5), 1);
                //set H level
	            pmu_set_gpio_value(GPIO_PORT_D, BIT(6), 1);
                break;

                default:
                break;
            }
          ;
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;             
}

/**
  * @brief  LEDClose
  * @note   xxx
  * @param  None
  * @retval None
  */
static long LEDClose (void *pvHandle)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;     
}



/**
  * @brief  g_stLEDCtlFUN
  * @note  xxx
  * @param  None
  * @retval None
  */
ST_CTR_FUN g_stLEDCtlFUN = 
{
    LEDOpen,		//打开
    LEDRead,		//读
    LEDWrite,		//写
    LEDControl,		//控制
    LEDClose    	//关闭
};


/******************* (C) COPYRIGHT 2011 ShenZhenShiding electronics **************************/
